package com.softmarket.service;

import java.util.Date;
import java.util.List;

import org.seasar.extension.jdbc.SqlBatchUpdate;

import com.softmarket.dto.DownloadPasswordDto;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class DownloadPasswordService extends JdbcManagerFactory {

	public boolean insert(DownloadPasswordDto dto) {

		boolean result = true;
		try {
			String sql = "INSERT INTO t_order_key VALUES(?,?,?,?,?,?)";
			SqlBatchUpdate batchUpdate = jdbcManager.updateBatchBySql(sql,
					String.class, String.class, String.class, String.class,
					Date.class, String.class);
			batchUpdate.params(dto.getId(), dto.getOrderType(), dto
					.getOrderNo(), dto.getDownloadKey(), dto.getCreateTime(),
					dto.getDeleteFlg());

			batchUpdate.execute();
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public boolean update(DownloadPasswordDto dto) {

		boolean result = true;
		try {
			String sql = "UPDATE softmarket.t_order_key SET create_time=? , download_key=? where order_no=?";
			SqlBatchUpdate batchUpdate = jdbcManager.updateBatchBySql(sql,
					Date.class, String.class, String.class);
			batchUpdate.params(dto.getCreateTime(), dto.getDownloadKey(), dto
					.getOrderNo());
			batchUpdate.execute();
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public List<DownloadPasswordDto> select(DownloadPasswordDto dto) {

		String sql = "SELECT  * FROM t_order_key" + " WHERE order_no = ?";
		List<DownloadPasswordDto> resultList = jdbcManager.selectBySql(
				DownloadPasswordDto.class, sql, dto.getOrderNo())
				.getResultList();
		return resultList;
	}

	public String getDownloadKey() {
		return Base64.encode(("" + System.currentTimeMillis()).getBytes())
				.substring(10, 18);
	}
}
